package org.example;

import java.io.*;

/**
 * 文件类（将数据保存或读入）
 */

public class SaveAndLoad {

    /**
     * 商品的文件加载
     */
    static void productLoad(){
        String filePath = "ProductList";
        try(FileReader fileReader = new FileReader(filePath)){
            BufferedReader reader = new BufferedReader(fileReader);
            String line;
            while((line = reader.readLine())!=null){
                String [] data = line.split(",");
                Product product = new Product(data[0],data[1],Double.parseDouble(data[2]),Integer.parseInt(data[3]));
                Supermarket.inventory.add(product);
            }
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

    /**
     * 商品的文件保存
     */
    static void productDeleteSave(){
        String fileName = "ProductList";
        File file = new File(fileName);
        if(file.delete()){
            for(Product product : Supermarket.inventory){
                String temp = product.getId()+","+product.getName()+","+product.getPrice()+","+product.getQuantity()+"\n";
                try (FileOutputStream fos = new FileOutputStream(fileName,true)){
                    fos.write(temp.getBytes());
                }catch (IOException e){
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    /**
     * 用户的钱的文件保存
     */
    static void myMoneyDeleteSave(double money){
        String fileName = "MyMoney";
        File file = new File(fileName);
        if(file.delete()) {
            try (FileOutputStream fos = new FileOutputStream(fileName, true)) {
                fos.write(Double.toString(money).getBytes());
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    /**
     * 用户的钱的文件加载
     */
    static void myMoneyLoad(){
        String fileName = "MyMoney";
        try(FileReader fileReader = new FileReader(fileName)){
            BufferedReader reader = new BufferedReader(fileReader);
            String line = reader.readLine();
            User.myMoney = Double.parseDouble(line);
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

    /**
     * 购物车的加载
     */
    static void myPurchaseLoad(){
        String filePath = "MyPurchase";
        try(FileReader fileReader = new FileReader(filePath)){
            BufferedReader reader = new BufferedReader(fileReader);
            String line;
            while((line = reader.readLine())!=null){
                String [] data = line.split(",");
                Product product = new Product(data[0],Integer.parseInt(data[1]));
                User.myPurchase.add(product);
            }
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

    /**
     * 用户购物车的文件保存
     */
    static void myPurchaseDeleteSave(){
        String fileName = "MyPurchase";
        File file = new File(fileName);
        if(file.delete()) {
            try (FileOutputStream fos = new FileOutputStream(fileName, true)) {
                for(Product product : User.myPurchase){
                    String temp = product.getName()+","+product.getQuantity()+"\n";
                    try {
                        fos.write(temp.getBytes());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
